package com.yr.ryjk.expert.service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.common.Pagination;
import com.yr.ryjk.doctor.dao.DoctorMapper;
import com.yr.ryjk.doctor.entity.FilterParam;
import com.yr.ryjk.doctor.vo.DoctorVOCensus;
import com.yr.ryjk.expert.dao.DoctorExpertMapper;
import com.yr.ryjk.expert.dao.ExpertTypeMapper;
import com.yr.ryjk.expert.entity.DoctorExpert;
import com.yr.ryjk.expert.entity.ExpertType;
import com.yr.ryjk.redis.JedisUtil;
import com.yr.ryjk.user.dao.UserMapper;
import com.yr.ryjk.util.JSONUtil;

@Service
public class ExpertService implements IExpertService{
	@Autowired
	private ExpertTypeMapper expertTypeMapper;
	@Autowired
	private DoctorExpertMapper doctorExpertMapper;
	@Autowired
	private DoctorMapper doctorMapper;
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public Map<String, Object> getAll(int pageNo, int pageSize) {
		try{
			int count = expertTypeMapper.selectCount();
			List<ExpertType> list = expertTypeMapper.selectAll(pageNo, pageSize);
			 
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("list", list);
			map.put("pagination", new Pagination(pageSize,pageNo,count));
			return map;
		}catch (Exception e) {
			e.printStackTrace();
			return JSONUtil.getJSON(ErrorType.SERVER_ERROR);
		}
		
	}

	@Override
	public Map<String, Object> addInfo(ExpertType expertType)
			throws IllegalAccessException, InvocationTargetException {
		expertTypeMapper.insert(expertType);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	@Override
	public void updateInfo(ExpertType expertType) {
		expertTypeMapper.updateByPrimaryKey(expertType);
	}

	@Override
	public Map<String, Object> getAllExpertTypeOfDoctor(String docId) {
		List<DoctorExpert> doctorExperts = doctorExpertMapper.selectByDoctorId(docId);
		List<ExpertType> list = new ArrayList<ExpertType>();
		for(DoctorExpert doctorExpert : doctorExperts){
			list.add(expertTypeMapper.selectByPrimaryKey(doctorExpert.getExpertTypeId()));
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		return map;
	}

	@Override
	public Map<String, Object> addDoctorExpertInfo(DoctorExpert doctorExpert) throws IllegalAccessException, InvocationTargetException {
		List<DoctorExpert> list = doctorExpertMapper.selectByDoctorId(doctorExpert.getDocId());
		for(DoctorExpert det : list){
			if(det.getDocId().equals(doctorExpert.getDocId())&&det.getExpertTypeId().equals(doctorExpert.getExpertTypeId())){
				return JSONUtil.getJSON(ErrorType.DOCTOR_ALREADY_IN_EXPERT_ERROR);
			}
		}	
		doctorExpertMapper.insert(doctorExpert);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> deleteByDocId(String typeId,String docIds) {
		doctorExpertMapper.deletePopDoc(typeId,docIds);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
		
	}

	@Override
	public List<ExpertType> getAllExpertType() {
		return expertTypeMapper.selectAllExpertType();
	}

	@Override
	public Map<String, Object> getListOfType(int pageNo, int pageSize, String typeId,String token,FilterParam filterParam) {	
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("start", (pageNo-1)*pageSize);
		params.put("limit", pageSize);
		params.put("typeId", typeId);
		params.put("hospitalLevel", filterParam.getHospitalLevel());
		params.put("departmentId", filterParam.getDepartmentId());
		params.put("dutyId", filterParam.getDutyId());
		
		List<DoctorVOCensus> list = doctorMapper.selectDoctorVOCensusByTypeId(params);
		int count=doctorMapper.selectCountDoctorVOCensusByDocId(params);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("pagination", new Pagination(pageSize, pageNo,count));
		
		if(token!=null){   //app端请求,需要返回用户健康币信息
			String phone=JedisUtil.getPhoneByToken(token);
			int coin=userMapper.selectCoinByPhone(phone);
			map.put("coin", coin);
		}
		return map;
	}

	@Override
	public Map<String, Object> getInfoById(String id) {
		ExpertType expertType = expertTypeMapper.selectByPrimaryKey(id);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("expertType", expertType);    
		return map;
	}

	@Override
	public void deleteByType(String ids) {
		doctorExpertMapper.deleteByTypeIds(ids);
		expertTypeMapper.deleteByIds(ids);
	}
}
